package com.xbongbong.saas.analytical.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.analytical.SaasValidateAnalyticalService;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CompetitorEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.help.SaasDataInitHelp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author: yingjf
 * @date: 2022/9/2 15:27
 * @description:
 */
@Slf4j
@Service("competitorValidateAnalyticalServiceImpl")
public class CompetitorValidateAnalyticalServiceImpl implements SaasValidateAnalyticalService {
    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.COMPETITOR.getCode());
    }

    @Override
    public void analyticalData(ValidateDataDTO validateDataDTO) throws XbbException {
        try {
            JSONObject data = validateDataDTO.getData();
            JSONArray deptArray = data.getJSONArray(CompetitorEnum.VISBLE_DEPT.getAttr());
            if (Objects.nonNull(deptArray)) {
                List<Object> deptList = deptArray.toJavaList(Object.class);
                validateDataDTO.setVisbleDept(deptList);
            }
            JSONArray userArray = data.getJSONArray(CompetitorEnum.VISBLE_USER.getAttr());
            if (Objects.nonNull(userArray)) {
                List<Object> userList = userArray.toJavaList(Object.class);
                validateDataDTO.setVisbleUser(userList);
            }
            /* fix 53767 老审批--讲流转条件设定是可见人和可见部门后，不触发审批
            data.remove(CompetitorEnum.VISBLE_DEPT.getAttr());
            data.remove(CompetitorEnum.VISBLE_USER.getAttr());
             */
        } catch (Exception e) {
            log.error("com.xbongbong.saas.analytical.impl.CompetitorValidateAnalyticalServiceImpl.analyticalData error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }
}
